package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    NursingLevelMapper nursingLevelMapper;

    @Autowired
    NursingPlanMapper nursingPlanMapper;


    /**
     * 查询所有计划
     *
     * @return
     */
    @Override
    public List<NursingLevelVo> getAllLevel() {
        List<NursingLevelVo> nursingLevelVos = nursingLevelMapper.getAllLevel();
        return nursingLevelVos;
    }

    /**
     * 新增
     * @param nursingLevelDto
     */
    @Override
    public void addNursingLevel(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevelByName = nursingLevelMapper.selectByName(nursingLevelDto.getName());
        if (nursingLevelByName != null) {
            throw new BaseException(BasicEnum.NURSING_LEVEL_EXIST_ERROR);
        } else {
            NursingLevel nursingLevel = new NursingLevel();
            BeanUtil.copyProperties(nursingLevelDto, nursingLevel);
            nursingLevelMapper.addNursingLevel(nursingLevel);
        }
    }


    /**
     * 条件分页查询
     *
     * @return
     */
    @Override
    public PageResponse<NursingLevelVo> selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingLevelVo> page = nursingLevelMapper.selectByPage(name, status);
        return PageResponse.of(page, NursingLevelVo.class);
    }


    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public NursingLevelVo getById(Long id) {
        NursingLevel nursingLevel = nursingLevelMapper.getById(id);
        log.info("数据{}",nursingLevel);
        NursingLevelVo nursingLevelVo = new NursingLevelVo();
        BeanUtil.copyProperties(nursingLevel, nursingLevelVo);
        return nursingLevelVo;
    }


    /**
     * 修改
     *
     * @param nursingLevelDto
     */
    @Override
    public void modify(NursingLevelDto nursingLevelDto) {
        // todo 与老人进行绑定则不可修改
        NursingLevel nursingLevelByName = nursingLevelMapper.selectByName(nursingLevelDto.getName());
        /*if (nursingLevelByName.getStatus() != 0) {
            throw new BaseException(BasicEnum.NURSING_LEVEL_STATUS_ERROR);
        }*/
        Long count = nursingLevelMapper.countDiffBedNumber(nursingLevelDto.getName(), nursingLevelDto.getId());
        if (count > 0) {
            throw new BaseException(BasicEnum.NURSING_LEVEL_EXIST_ERROR);
        }

        NursingLevel nursingLevel = nursingLevelMapper.getById(nursingLevelDto.getId());
        log.info("数据{}",nursingLevel);
        nursingLevelMapper.modify(nursingLevel);

    }


    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void deleteLevel(Long id) {
        // todo 与老人进行绑定则不可修改
        nursingLevelMapper.deleteLevel(id);
    }


    /**
     * 启用禁用
     *
     * @param id
     * @param status
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        // todo 与老人进行绑定则不可修改
        nursingLevelMapper.enableOrDisable(id, status);
    }


}
